package com.simple.datax.transform.tcs;

import org.apache.xmlbeans.XmlObject;

import com.simple.datax.SimpleMessage;
import com.simple.datax.SimpleObject;
import com.simple.datax.api.TransformerException;
import com.simple.datax.transform.SimpleTransformer;
import com.simple.datax.xml.ec.EC101MessageDocument;
import com.simple.datax.xml.ec.EC101ResponseDocument;
import com.simple.datax.xml.tcs.TCS101MessageDocument;
import com.simple.datax.xml.tcs.TCS101ResponseDocument;

/**
 * @Title: Tcs2EcTransformerImpl.java
 * @Package com.simple.datax.transform.tcs
 * @Description: TCS到Ec的报文转换
 * @author Paladin Chen
 * @date 2013-1-9 下午3:17:05
 * @version V1.0
 */
public class Tcs2EcTransformerImpl implements SimpleTransformer {
	private boolean isAcceptNull = false;

	private boolean isIgnoreBadInput = false;

	private String encoding = "UTF-8";

	@Override
	public boolean isAcceptNull() {
		return isAcceptNull;
	}

	public void setAcceptNull(boolean isAcceptNull) {
		this.isAcceptNull = isAcceptNull;
	}

	public void setIgnoreBadInput(boolean isIgnoreBadInput) {
		this.isIgnoreBadInput = isIgnoreBadInput;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	@Override
	public String getEncoding() {
		return encoding;
	}

	@Override
	public boolean isIgnoreBadInput() {
		return isIgnoreBadInput;
	}

	@Override
	public void transform(SimpleObject src) throws TransformerException {
		transform(src, encoding);

	}

	@Override
	public void transform(SimpleObject src, String encoding) throws TransformerException {
		if (src instanceof SimpleMessage) {
			XmlObject intraDoc = ((SimpleMessage) src).getIntraDoc();

			if (intraDoc instanceof TCS101MessageDocument) {
				Tcs2EcMessageTransformer t2emsgtrans = new Tcs2EcMessageTransformer();
				EC101MessageDocument ecMsgDoc = t2emsgtrans.transform((TCS101MessageDocument) intraDoc);

				((SimpleMessage) src).setIntraDoc(ecMsgDoc);

			} else if (intraDoc instanceof TCS101ResponseDocument) {
				Tcs2EcResponseTransformer t2eRespTrans = new Tcs2EcResponseTransformer();
				EC101ResponseDocument ecRespDoc = t2eRespTrans.transform((TCS101ResponseDocument) intraDoc);
				
				((SimpleMessage) src).setIntraDoc(ecRespDoc);

			} else {
				throw new TransformerException("The MsgType is not supported now!");
			}

		} else {
			throw new TransformerException("The Paramter MUST be SimpleMessage!");
		}

	}
}
